%% 这一部分检查旋转频率跟踪器的输出是否正确

%% 1. 加载配置（信号+接收机）
sim_totalDuration = signalConfig.sim.duration;   % 总仿真时长
sim_startTime = 0;      % 开始时间
sim_interval = rxConfig.tracker.totalIntegTime;   % 采样间隔(秒)

%% 2. 初始化模型
modulatorModel = SignalModulator(signalConfig, rxConfig); 
demodulatorModel = RxTrackingLoop(signalConfig, rxConfig);  

%% 3. 调制和解调
totalSteps = round((sim_totalDuration) / sim_interval) + 1;

%% 4. 初始化进度显示类
progressDisplayer = ProgressDisplayer(...
    '调制解调测试仿真：', ...  % 自定义标题
    totalSteps, ...            % 总步数（与循环步数一致）
    100, ...                   % 进度刻度（100表示百分比，可改为总步数显示）
    false);                    % 不禁用显示

progressDisplayer.start();

%% 5. 初始化记录变量（预分配）
fqyerrList   = zeros(totalSteps,1);
pherrList    = zeros(totalSteps,1);
delayerrList = zeros(totalSteps,1);
fqyList      = zeros(totalSteps,1);
phList       = zeros(totalSteps,1);
delayList    = zeros(totalSteps,1);
cndrList     = zeros(totalSteps,1);
rotfreqList  = zeros(totalSteps,1);
rotAList     = zeros(totalSteps,1);
rotAlphaList = zeros(totalSteps,1);
tMeasureList = zeros(totalSteps,1);
phlockoList  = zeros(totalSteps,1);
phlockpList  = zeros(totalSteps,1);
rotFqyErrList = zeros(totalSteps,1);

for curStep = 1:totalSteps
    t = sim_startTime + (curStep - 1) * sim_interval;

    [waveformRx, tRx] = modulatorModel.step();
    [result, performationIndex] = demodulatorModel.step(waveformRx);

    progressDisplayer.step(1);

    fqyerr   = result.frequencyError;
    pherr    = result.phaseError;
    delayerr = result.codeError;
    fqy      = result.frequency;
    ph       = result.phase;
    delay    = result.delay;
    cndr     = result.cndr;
    rotfreq  = result.rotFreqEst;
    rotA     = result.rotAEst;
    rotAlpha = result.rotAlphaEst;
    tMeasure = performationIndex.tMeasure;
    phlocko  = performationIndex.phlocko;
    phlockp  = performationIndex.phlockp;
    rotFqyErr = result.rotFqyErr;

    % 直接按索引赋值
    fqyerrList(curStep)   = fqyerr;
    pherrList(curStep)    = pherr;
    delayerrList(curStep) = delayerr;
    fqyList(curStep)      = fqy;
    phList(curStep)       = ph;
    delayList(curStep)    = delay;
    cndrList(curStep)     = cndr;
    rotfreqList(curStep)  = rotfreq;
    rotAList(curStep)     = rotA;
    rotAlphaList(curStep) = rotAlpha;
    tMeasureList(curStep) = tMeasure;
    phlockoList(curStep)  = phlocko;
    phlockpList(curStep)  = phlockp;
    rotFqyErrList(curStep) = rotFqyErr;
end


%% 6. 绘制结果
% 真值计算
[phGenie, fqyGenie, delayGenie, cndrGenie, rotfreqGenie] = getGenie(signalConfig,rxConfig ,tMeasureList);
% 绘制估计的频率、相位、延迟 与对应真值的对比图
winLen = 32;
fontsize = 12;
groupTitle = '调制解调测试';
plotPLL(groupTitle, tMeasureList, pherrList, phList, phGenie, phlockoList, phlockpList, winLen, fontsize);
plotFLL(groupTitle, tMeasureList, fqyerrList,  fqyList, fqyGenie, winLen, fontsize);
plotDLL(groupTitle, tMeasureList, delayerrList, delayList, delayGenie, signalConfig.signal.codeLen, winLen, fontsize);
plotRotFreq(groupTitle, tMeasureList, rotFqyErrList, rotfreqList, rotfreqGenie, fontsize, rxConfig.tracker.rotationFreq.T_roll);
